package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.exception.LeaseException;
import com.atguigu.lease.common.result.Result;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.model.enums.ReleaseStatus;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.atguigu.lease.web.admin.vo.fee.FeeValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {
    @Autowired
    private GraphInfoService graphInfoService;
    @Autowired
    private ApartmentFacilityService apartmentFacilityService;
    @Autowired
    private ApartmentLabelService apartmentLabelService;
    @Autowired
    private ApartmentFeeValueService apartmentFeeValueService;

    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;
    @Autowired
    private FacilityInfoMapper facilityInfoMapper;

    @Autowired
    private LabelInfoMapper labelInfoMapper;

    @Autowired
    private FeeValueMapper feeValueMapper;

    @Autowired
    private GraphInfoMapper graphInfoMapper;

    @Autowired
    private RoomInfoService roomInfoService;



    @Override
    public void customsaveOrUpdate(ApartmentSubmitVo apartmentSubmitVo) {
        boolean iu = apartmentSubmitVo.getId()!=null;
        saveOrUpdate(apartmentSubmitVo);
        if (iu){
            //图片删除
            LambdaQueryWrapper<GraphInfo> groupInfoLambdaQueryWrapper =  new LambdaQueryWrapper<>();
            groupInfoLambdaQueryWrapper.eq(GraphInfo::getItemId,apartmentSubmitVo.getId());
            groupInfoLambdaQueryWrapper.eq(GraphInfo::getItemType, ItemType.APARTMENT);
            graphInfoService.remove(groupInfoLambdaQueryWrapper);
            //配套删除
            LambdaQueryWrapper<ApartmentFacility> apartmentFacilityLambdaQueryWrapper=new LambdaQueryWrapper<>();
            apartmentFacilityLambdaQueryWrapper.eq(ApartmentFacility::getApartmentId,apartmentSubmitVo.getId());
            apartmentFacilityService.remove(apartmentFacilityLambdaQueryWrapper);
            //标签删除
            LambdaQueryWrapper<ApartmentLabel> apartmentLabelLambdaQueryWrapper=new LambdaQueryWrapper<>();
            apartmentLabelLambdaQueryWrapper.eq(ApartmentLabel::getApartmentId,apartmentSubmitVo.getId());
            apartmentLabelService.remove(apartmentLabelLambdaQueryWrapper);
            //杂费删除
            LambdaQueryWrapper<ApartmentFeeValue>  apartmentFeeValueLambdaQueryWrapper =new LambdaQueryWrapper<>();
            apartmentFeeValueLambdaQueryWrapper.eq(ApartmentFeeValue::getApartmentId,apartmentSubmitVo.getId());
            apartmentLabelService.remove(apartmentLabelLambdaQueryWrapper);
        }
        //图片保存
        List<GraphVo> graphVoList = apartmentSubmitVo.getGraphVoList();
        if(!CollectionUtils.isEmpty(graphVoList)){
            List<GraphInfo> graphInfoList = new ArrayList<>(graphVoList.size());
            for(GraphVo graphVo : graphVoList){
                GraphInfo graphInfo = new GraphInfo();
                graphInfo.setName(graphVo.getName());
                graphInfo.setUrl(graphVo.getUrl());
                graphInfo.setItemType(ItemType.APARTMENT);
                graphInfo.setItemId(apartmentSubmitVo.getId());
                graphInfoList.add(graphInfo);
            }
            graphInfoService.saveBatch(graphInfoList);
        }
        //配套保存
        List<Long> facilityInfoIds = apartmentSubmitVo.getFacilityInfoIds();
        if(!CollectionUtils.isEmpty(facilityInfoIds)){
            List<ApartmentFacility> apartmentFacilityList = new ArrayList<>(facilityInfoIds.size());
            for(Long facilityInfoId : facilityInfoIds){
                ApartmentFacility facility = ApartmentFacility.builder().
                        facilityId(facilityInfoId).apartmentId(apartmentSubmitVo.getId()).build();
                apartmentFacilityList.add(facility);
            }
            apartmentFacilityService.saveBatch(apartmentFacilityList);
        }
        //标签保存
        List<Long> labelIds = apartmentSubmitVo.getLabelIds();
        if(!CollectionUtils.isEmpty(labelIds)){
            List<ApartmentLabel> apartmentLabelList = new ArrayList<>(labelIds.size());
            for(Long labelId : labelIds){
                ApartmentLabel apartmentLabel = ApartmentLabel.builder()
                        .labelId(labelId).apartmentId(apartmentSubmitVo.getId()).build();
                apartmentLabelList.add(apartmentLabel);
            }
            apartmentLabelService.saveBatch(apartmentLabelList);
        }
        //杂费保存
        List<Long> feeValueIds = apartmentSubmitVo.getFeeValueIds();
        if (!CollectionUtils.isEmpty(feeValueIds)){
            List<ApartmentFeeValue> apartmentFeeValueList = new ArrayList<>(feeValueIds.size());
            for (Long feeValueId : feeValueIds) {
                ApartmentFeeValue apartmentFeeValue  = ApartmentFeeValue.builder().feeValueId(feeValueId)
                        .apartmentId(apartmentSubmitVo.getId()).build();
                apartmentFeeValueList.add(apartmentFeeValue);
            }
            apartmentFeeValueService.saveBatch(apartmentFeeValueList);
        }
    }

    @Override
    public void queryPage(Page<ApartmentItemVo> page, ApartmentQueryVo queryVo) {

        apartmentInfoMapper.queryPage(page,queryVo);
    }

    @Override
    public ApartmentDetailVo queryDetail(Long id) {
        //1.查询基本信息
        ApartmentInfo apartmentInfo = this.getById(id);
        if(apartmentInfo==null){
            return null;
        }
        //2.查询公寓配套
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.queryByApartmentId(id);
        //3.公寓id查询图片集合
        List<GraphVo> graphVoList = graphInfoMapper.queryByItemId(id, ItemType.APARTMENT);
        //4.根据公寓id查询标签集合
        List<LabelInfo> labelInfoList = labelInfoMapper.queryByApartmentId(id);
        //5.公寓id查询杂费和杂费名集合
        List<FeeValueVo> feeValueVoList = feeValueMapper.queryByAdaptmentId(id);
        //6.合并到ApartmentDetailVo对象返回即可
        ApartmentDetailVo detailVo = new ApartmentDetailVo();
        detailVo.setFacilityInfoList(facilityInfoList);
        detailVo.setGraphVoList(graphVoList);
        detailVo.setLabelInfoList(labelInfoList);
        detailVo.setFeeValueVoList(feeValueVoList);
        BeanUtils.copyProperties(apartmentInfo, detailVo);
        return detailVo;
    }

    @Override
    public void removeApartmentById(Long id){
    //判断是否有房间
        LambdaQueryWrapper<RoomInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(RoomInfo::getApartmentId, id);
        long count = roomInfoService.count(lambdaQueryWrapper);
        if (count > 0) {
            //抛出异常
            throw new LeaseException(ResultCodeEnum.DELETE_ERROR);
        }
        //1.删除公寓
        removeById(id);
        //2.删除公寓相关的配套
        LambdaQueryWrapper<ApartmentFacility> apartmentFacilityLambdaQueryWrapper = new LambdaQueryWrapper<>();
        apartmentFacilityLambdaQueryWrapper.eq(ApartmentFacility::getApartmentId, id);
        apartmentFacilityService.remove(apartmentFacilityLambdaQueryWrapper);

        //3.删除公寓相关的标签
        LambdaQueryWrapper<ApartmentLabel> apartmentLabelLambdaQueryWrapper = new LambdaQueryWrapper<>();
        apartmentLabelLambdaQueryWrapper.eq(ApartmentLabel::getApartmentId, id);
        apartmentLabelService.remove(apartmentLabelLambdaQueryWrapper);

        //4.删除公寓相关图片
        LambdaQueryWrapper<GraphInfo> apartmentGraphLambdaQueryWrapper = new LambdaQueryWrapper<>();
        apartmentGraphLambdaQueryWrapper.eq(GraphInfo::getItemType, ItemType.APARTMENT);
        graphInfoService.remove(apartmentGraphLambdaQueryWrapper);

        //5.删除公寓相关的杂费
        LambdaQueryWrapper<ApartmentFeeValue> apartmentFeeValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
        apartmentFeeValueLambdaQueryWrapper.eq(ApartmentFeeValue::getApartmentId, id);
        apartmentFeeValueService.remove(apartmentFeeValueLambdaQueryWrapper);
    }

    @Override
    public void updateStatusById(Long id, ReleaseStatus status) {
        LambdaUpdateWrapper<ApartmentInfo> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(ApartmentInfo::getId, id);
        updateWrapper.set(ApartmentInfo::getIsRelease, status);
        update(updateWrapper);
    }

    @Override
    public List<ApartmentInfo> getApartmentDetailByDistrictId(Long id) {
        LambdaQueryWrapper<ApartmentInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ApartmentInfo::getDistrictId,id);
        List<ApartmentInfo> list = list(lambdaQueryWrapper);
        return list;
    }


}




